/*
 * Copyright (c) 2011, Eriq Muhammad Adams J.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Institute Technology of Sepuluh Nopember nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ERIQ MUHAMMAD ADAMS J. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.pathmachine.ui.renderer.impl;

import com.googlecode.pathmachine.mm.Map;
import com.googlecode.pathmachine.mm.Point3D;
import com.googlecode.pathmachine.ui.renderer.PathRenderer;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author <a href="mailto:eriq.adams@gmail.com">Eriq Muhammad Adams J.</a> 
 */
public class PathRendererImpl implements PathRenderer {

    private ColorRGBA pathColor = ColorRGBA.Orange;

    public Node render(List<Integer> path, Map map, AssetManager assetManager) {
        Node node = new Node("pathDebug");

        List<Point3D> startPoints = new ArrayList<Point3D>();
        List<Point3D> endPoints = new ArrayList<Point3D>();
        int size = path.size();
        for (int i = 0; i < size; i++) {
            if (i + 1 < size) {
                int s = path.get(i);
                int e = path.get(i + 1);
                Point3D sp = map.getOpenGLCoord(s >> 16, s & 0xFFFF);
                Point3D ep = map.getOpenGLCoord(e >> 16, e & 0xFFFF);
                startPoints.add(sp);
                endPoints.add(ep);
            }

        }

        // Draw Grid Edges
        Edges edges = new Edges(startPoints, endPoints);
        edges.setLineWidth(6f);
        Geometry edgesGeom = new Geometry("edges", edges);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.getAdditionalRenderState().setWireframe(false);
        mat.setColor("Color", pathColor);
        edgesGeom.setMaterial(mat);
        edgesGeom.move(0, +0.01f, 0);
        node.attachChild(edgesGeom);
        startPoints = null;
        endPoints = null;

        return node;
    }

    public void setPathColor(ColorRGBA color) {
        pathColor = color;
    }
}
